<template>
    <el-form class="routerView" :model="student" label-width="auto" size="medium" label-position="left">  
        <el-form-item label="学号：">
            <el-input v-model="student.sid"></el-input>
        </el-form-item>
        <el-form-item label="密码：">
            <el-input v-model="student.password"></el-input>
        </el-form-item>
        <el-form-item label="姓名：">
            <el-input v-model="student.name"></el-input>
        </el-form-item>
        <el-form-item label="性别：">
            <el-radio v-model="student.sex" label="男">男</el-radio>
            <el-radio v-model="student.sex" label="女">女</el-radio>
        </el-form-item>
        <el-form-item label="专业:">
            <el-input v-model="student.dept"></el-input>
        </el-form-item>
        <el-form-item label="宿舍号：">
            <el-select @click.native="loadDorids()" v-model="student.dorid">
                <el-option v-for="dorid in dorids" :key="dorid" :value="dorid"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="电话：">
            <el-input maxlength="11" v-model="student.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱：">
            <el-input v-model="student.email"></el-input>
        </el-form-item>
        <div class="button">
            <el-form-item size="large" label-width="0">
                <el-button type="primary" @click="onSubmit(student)">修改</el-button>
            </el-form-item>
            <el-form-item size="large" label-width="0">
                <el-button type="primary" @click="cancel()">取消</el-button>
            </el-form-item>
        </div>
    </el-form>
</template>

<script>
export default {
    name:'Edithomemaster',
    data() {
        return {
            student:{},
            dorids:[]
        }
    },
    methods:{
        load(){
            let _this = this
            axios.get(`http://localhost:8081/student/getStudentById/${_this.$route.query.sid}`).then(function(resp){
                _this.student = resp.data
                _this.loadDorids()
            })            
        },
        loadDorids(){
            let _this = this
            axios.get(`http://localhost:8081/dormitory/getEmptyDorid/${_this.student.sex}`).then(function(resp){
                _this.dorids = resp.data
            })
        },
        onSubmit(student){
            let _this = this
            let k = 0
            let arr = Array('qq.com','163.com','139.com','sina.com','yeah.com','126.com','21cn.com','gmail.com','yahoo.com','msn.com','hotmail.com','aol.com','ask.com','live.com','3721.net','googlemail.com','mail.com','0355.net')
            if(student.sid == '' || student.password == '' || student.name == '' || student.phone == '' || student.email == ''){
                alert("输入不能为空");
                return;
            }
            if((student.sex == '男' && parseInt(student.dorid.split('#')[0])<200) || (student.sex == '女' && parseInt(student.dorid.split('#')[0])>200)){
                alert("性别和宿舍对不上")
                return
            }
            if(student.phone.length < 11 || isNaN(parseInt(student.phone)) || student.phone.slice(0,1) != '1'){
                alert('电话输入有误')
                return
            }
            if(student.email.split('@')[0] == '' || !arr.includes(student.email.split('@')[1])){
                alert('邮箱地址有误')
                return
            }
            axios.get(`http://localhost:8081/student/editStudent/${this.$route.query.sid}`,{params:_this.student}).then(function(resp){
                axios.get(`http://localhost:8081/dormitory/delete1PoAtDor/${_this.$route.query.building}/${_this.$route.query.room}`,).then(function(){
                    axios.get(`http://localhost:8081/dormitory/add1PoAtDor/${_this.student.dorid.split("#")[0]}/${_this.student.dorid.split("#")[1]}`).then(function(){
                        alert("修改成功")
                        _this.$route.query.sid = _this.student.sid
                        _this.$route.query.building = _this.student.dorid.split("#")[0]
                        _this.$route.query.room = _this.student.dorid.split("#")[1]
                    }).catch(function(){
                    alert('修改失败3')
                    })
                }).catch(function(error){
                    alert('修改失败2')
                })
            }).catch(function(error){
                alert('修改失败1')
            })
        },
        cancel(){
            this.$router.push('/studentmannager')
        }
    },
    mounted(){
        this.load();
        }
}
</script>

<style scoped>
    .el-form{
        width: 500px;
    }
    .el-form-item{
        text-align: left;
    }
    .el-input{
        display: inline-block;
        font-size: 14px;
    }
    .bigsize{
        font-size: 20px;
    }
    .button{
        display: flex;
        justify-content: space-evenly;
    }
    .routerView{
        padding: 20px;
        border-radius: 5px;
        position: relative;
        background-color: #fff;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        box-shadow: 3px 3px 6px 2px rgba(0, 0, 0, 0.5);
    }
</style>